<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="信息标题" prop="messageName">
        <el-input
          v-model="queryParams.messageName"
          placeholder="请输入信息标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信息类型" prop="messageTypeId">
        <el-select v-model="queryParams.messageTypeId" placeholder="请输入信息类型" clearable >
          <el-option
            v-for="dict in dict.type.message_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="addTime">
        <el-date-picker clearable
          v-model="queryParams.addTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间" style="width: 160px;">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建者" prop="userId">
        <el-select
          v-model="queryParams.userId"
          clearable
          @keyup.enter.native="handleQuery"
          filterable
          placeholder="请输入或选择创建者"
        >
          <el-option
            v-for="item in acceptor"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:message:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:message:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:message:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:message:export']"
        >导出</el-button>
      </el-col>
        <audio controls="controls" hidden src="../voice/zhendong.mp3" ref="audio1"></audio>
        <audio controls="controls" hidden src="../voice/tips.mp3" ref="audio2"></audio>
        <audio controls="controls" hidden src="../voice/delete.mp3" ref="audio3"></audio>
        <audio controls="controls" hidden src="../voice/Download.mp3" ref="audio4"></audio>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="信息编号" align="center" type="index"  width="105"/>
      <el-table-column label="信息标题" align="center" prop="messageName" :show-overflow-tooltip="true"/>
      <el-table-column label="信息类型" align="center" prop="dictLabel" />
      <el-table-column label="信息内容" align="center" prop="messageContent" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="addTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="nickName" />
      <el-table-column label="审批" align="center" prop="approval" v-if="showPrise">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="Update(scope.row)"
            id="bt1"
          >{{ scope.row.approval==1?"已审批":"未审批" }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="memo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:message:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:message:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:message:query']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信息发布对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="信息标题" prop="messageName">
          <el-input v-model="form.messageName" placeholder="请输入信息标题" />
        </el-form-item>
        <el-form-item label="信息类型" prop="messageTypeId">
            <el-select v-model="form.messageTypeId" placeholder="请选择信息类型" >
              <el-option
               v-for="dict in dict.type.message_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
             />
          </el-select>
        </el-form-item>
        <el-form-item label="信息内容" prop="messageContent">
          <el-input v-model="form.messageContent" :min-height="192" type="textarea"/>
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

        <!-- 查看详情 -->
        <el-dialog title="查看详情" :visible.sync="openView" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-row>
          <el-col :span="24">
            <el-form-item label="信息标题：">{{ form.messageName }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="信息类型：">{{ form.dictLabel }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="信息内容：" style="white-space: pre-line;">{{ form.messageContent }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listMessage, getMessage,getMess, delMessage, addMessage, updateMessage,updateMess } from "@/api/system/message";
import { getUserProfile,listUser } from "@/api/system/user";
export default {
  name: "Message",
  dicts: ['message_type'],
  data() {
    return {
      //创建者下拉列表
      acceptor: [],
      //审批字段隐藏
      showPrise:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息发布表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详细弹出层
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageName: null,
        messageTypeId: null,
        messageContent: null,
        addTime: null,
        userId: null,
        approval: null,
        memo: null,
        fakeDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        messageName: [
          { required: true, message: "信息标题不能为空", trigger: "blur" },
          { min: 2, max: 60, message: '信息标题长度必须介于 2 和 60 之间', trigger: 'blur' }
        ],
        messageTypeId:[
        { required: true, message: "信息类型不能为空", trigger: "blur" }
        ],
      },
      roleGroup: {},
    };
  },
  created() {
    this.getList();
    this.getUser();
    this.getAcceptor();
  },
  methods: {
    //受理人列表
    getAcceptor() {
      listUser(this.queryParams).then((response) => {
        this.acceptor = response.rows;
      });
    },
    /** 任务详细信息 */
    handleView(row) {
      this.$refs.audio2.currentTime = 0;
      this.$refs.audio2.play();
      getMessage(row.messageId).then(response => {
        this.form = response.data;
        this.openView = true;
      });
    },
    //登录账号权限查询
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        if(this.roleGroup==="超级管理员"){
          this.showPrise=true
        }
      });
    },
    /** 查询信息发布列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      for (let index = 0; index < this.messageList.length; index++) {
          if(this.messageList[index].approval==1){
            document.getElementById("bt1").disabled=true
          }
        }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        messageId: null,
        messageName: null,
        messageTypeId: null,
        messageContent: null,
        addTime: null,
        updateTime: null,
        userId: null,
        approval: null,
        memo: null,
        fakeDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.messageId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信息发布";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.audio3.currentTime=0;
      this.$refs.audio3.play();
      this.reset();
      const messageId = row.messageId || this.ids
      getMessage(messageId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信息发布";
      });
    },
    //审批方法
    Update(row){
      this.reset();
      const messageId = row.messageId
      getMess(messageId).then(response => {
        this.form = response.data;
        
        if(this.form.approval==0){
          this.$modal.confirm("是否审批").then(function() {
      }).then(() => {
        this.form.approval=1
        updateMess(this.form).then(response => {
              this.$modal.msgSuccess("已审批");
                    this.getList();
                  });
      }).catch(() => {});  
      }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.messageId != null) {
            updateMessage(this.form).then(response => {
              console.log(this.form.messageContent);
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$refs.audio3.currentTime=0;
      this.$refs.audio3.play();
      const messageIds = row.messageId || this.ids;
      this.$modal.confirm('是否确认删除信息发布编号为"' + messageIds + '"的数据项？').then(function() {
        return delMessage(messageIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$refs.audio4.currentTime=0;
      this.$refs.audio4.play();
      this.download('system/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
